﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

namespace NLBS.Data
{
   public class PluginData
    {
       public IList<Plugin> GetGuestBook()
       {
           Database db = DatabaseFactory.CreateDatabase("NLBSConnectionString");
           
           DbCommand dbCommand = db.GetSqlStringCommand(@"SELECT * FROM blog_plugin");
           IList<Plugin> lists = new List<Plugin>();
           using (IDataReader dataReader = db.ExecuteReader(dbCommand))
           {
               while (dataReader.Read())
               {
                   Plugin gutstBook = PopulatePluginFromIDataReader(dataReader);
                   lists.Add(gutstBook);
               }
           }
           return lists;
       }

       public int InsertPlugin(Plugin plugin)
       {
           Database db = DatabaseFactory.CreateDatabase("NLBSConnectionString");
           DbCommand dbCommand = db.GetSqlStringCommand(@"INSERT INTO blog_Plugin
            (Version, Name, Description, Author, PubDate, Enabled, BlogVersion, 
             AuthorEmail, AuthorHomePage, InstallCount, FileSize, FileUrl
            VALUES 
            (@Version,@Name,@Description,@Author,@PubDate,@Enabled,@BlogVersion,
             @AuthorEmail,@AuthorHomePage,@InstallCount,@FileSize,@FileUrl)
            SELECT @pluginID=@@IDENTITY");
           db.AddInParameter(dbCommand, "Version", DbType.String, plugin.Version);
           db.AddInParameter(dbCommand, "Name", DbType.String, plugin.Name);
           db.AddInParameter(dbCommand, "Description", DbType.String, plugin.Description);
           db.AddInParameter(dbCommand, "Author", DbType.String, plugin.Author);
           db.AddInParameter(dbCommand, "PubDate", DbType.String, plugin.PubDate);
           db.AddInParameter(dbCommand, "Enabled", DbType.Boolean, plugin.Enable);
           db.AddInParameter(dbCommand, "BlogVersion", DbType.String, plugin.BlogVersion);
           db.AddInParameter(dbCommand, "AuthorEmail", DbType.String, "xxx");
           db.AddInParameter(dbCommand, "AuthorHomePage", DbType.String, "xx");
           db.AddInParameter(dbCommand, "InstallCount", DbType.Int32, 1);
           db.AddInParameter(dbCommand, "FileSize", DbType.String, "xx");
           db.AddInParameter(dbCommand, "FileUrl", DbType.String, "xx");
           db.AddOutParameter(dbCommand, "pluginID", DbType.Int32, 10);
           db.ExecuteNonQuery(dbCommand);
           int result = Convert.ToInt32(db.GetParameterValue(dbCommand, "@pluginID"));
           return result;
       }

       public void DeletePlugin(int pluginID)
       {
           Database db = DatabaseFactory.CreateDatabase("NLBSConnectionString");
           DbCommand dbCommand = db.GetSqlStringCommand("DELETE FROM blog_Plugin WHERE (pluginID = @pluginID)");
           db.AddInParameter(dbCommand, "pluginID", DbType.Int32, pluginID);
           db.ExecuteNonQuery(dbCommand);
       }

       private Plugin PopulatePluginFromIDataReader(IDataReader dr)
       {
           Plugin pl = new Plugin();
           pl.Version = dr["Version"].ToString();
           pl.Name = dr["Name"].ToString();
           pl.BlogVersion = dr["Version"].ToString();
           pl.Description = dr["Desciption"].ToString();
           pl.PluginID =Convert.ToInt32(dr["PluginID"].ToString());
           pl.Author = dr["Author"].ToString();
           pl.PubDate = Convert.ToDateTime(dr["PubData"].ToString());
           pl.Enable = Convert.ToBoolean(dr["Enable"].ToString());
           return pl;
       }
    }
}
